import Vue from "vue";
import Vuex from "Vuex";

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        //项目id
        projectId: localStorage.getItem('projectId') || 0,

        //项目名
        projectName: localStorage.getItem('projectName') || '',

        //分组id
        apiGroupId: localStorage.getItem('apiGroupId') || 0,

        //项目分组
        apiGroup: [],

        //接口信息
        interfaceInfo: {},

        count: 0,

        students: [
            { id: 110, name: 'why', age: 18 },
            { id: 111, name: 'kobe', age: 21 },
            { id: 112, name: 'lucy', age: 25 },
            { id: 113, name: 'lilei', age: 30 }

        ]
    },

    getters: {
        greaterAgesSatus: state => {
            return state.students.filter(s => s.age >= 20)
        },

        greaterAgesCount: (state, getters) => {
            return getters.getters.greaterAgesSatus.length
        },

        stuByID: state => {
            return id => {
                state.students.find(s => s.id === id)
            }
        }

    },

    mutations: {
        //项目id
        SET_PROJECTID: (state, projectId) => {
            state.projectId = projectId
            localStorage.setItem('projectId', projectId)
        },

        //项目名
        SET_PROJECTNAME: (state, projectName) => {
            state.projectName = projectName
            localStorage.setItem('projectName', projectName)
        },
        //分组id
        SET_APIGROUPID: (state, apiGroupId) => {
            state.apiGroupId = apiGroupId
            localStorage.setItem('apiGroupId', apiGroupId)
        },

        //项目分组
        SET_APIGROUP: (state, apiGroup) => {
            state.apiGroup = apiGroup
            localStorage.setItem('apiGroup', apiGroup)
        },

        //接口信息
        SET_INTERFACEINFO: (state, interfaceInfo) => {
            this.interfaceInfo = interfaceInfo
            localStorage.setItem('interfaceInfo', interfaceInfo)
        },

        increment (stat) {
            state.count++

        },

        decrement (stat) {
            state.count--

        },

        decrement (stat, n) {
            state.count -= n

        },
        changeCount (state, payload) {
            state.count = payload.count
        }
    },

    actions: {
        increment (context, payload) {
            setTimeout(() => {
                context.commit('increment', payload)
            }, 1000)
        },

        increment (context) {
            return new Promise((resolve) => {
                setTimeout(() => {
                    context.commit('increment')
                    resolve()
                }, 1000)

            })
        }
    }
})


export default store

